package com.shuidihuzhu.sdb.helper.log.utils;

import org.springframework.core.annotation.AnnotationUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 注解工具类
 * @author dongzhihua
 */
public class AnnotationUtil {
    private AnnotationUtil() {
    }

    /**
     * 先从方法上获取注解，如果没有从类上获取
     * @param method 方法
     * @param clazz 类型
     * @param <T> 注解类型
     * @return 注解
     */
    public static <T extends Annotation> T getAnnotationFromTypeOrMethod(Method method, Class<T> clazz) {
//        final T annotation = AnnotationUtils.getAnnotation(method, clazz);
//        if (annotation != null) {
//            return annotation;
//        }
//        return AnnotationUtils.getAnnotation(method.getDeclaringClass(), clazz);
        return getAnnotationFromTypeOrMethod2(method, clazz);
    }
    public static <T extends Annotation> T getAnnotationFromTypeOrMethod2(Method method, Class<T> clazz) {
        final T annotation = method.getAnnotation(clazz);
        if (annotation != null) {
            return annotation;
        }
        return method.getDeclaringClass().getAnnotation(clazz);
    }
}
